{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def draw_graph(G, node_names={}, node_size=500):\n",
    "    pos_nodes = nx.spring_layout(G)\n",
    "    nx.draw(G, pos_nodes, with_labels=True, node_size=node_size, edge_color='gray', arrowsize=30)\n",
    "    \n",
    "    pos_attrs = {}\n",
    "    for node, coords in pos_nodes.items():\n",
    "        pos_attrs[node] = (coords[0], coords[1] + 0.08)\n",
    "        \n",
    "    #nx.draw_networkx_labels(G, pos_attrs, font_family='serif', font_size=20)\n",
    "    \n",
    "    plt.axis('off')\n",
    "    axis = plt.gca()\n",
    "    axis.set_xlim([1.2*x for x in axis.get_xlim()])\n",
    "    axis.set_ylim([1.2*y for y in axis.get_ylim()])\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Graph Factorization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "\n",
    "G = nx.barbell_graph(m1=3, m2=2)\n",
    "draw_graph(G)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "Path(\"gem/intermediate\").mkdir(parents=True, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gem.embedding.gf import GraphFactorization\n",
    "\n",
    "G = nx.barbell_graph(m1=10, m2=4)\n",
    "draw_graph(G)\n",
    "\n",
    "gf = GraphFactorization(d=2,  data_set=None,max_iter=10000, eta=1*10**-4, regu=1.0)\n",
    "gf.learn_embedding(G)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "\n",
    "for x in G.nodes():\n",
    "    \n",
    "    v = gf.get_embedding()[x]\n",
    "    ax.scatter(v[0],v[1], s=1000)\n",
    "    ax.annotate(str(x), (v[0],v[1]), fontsize=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## GraphRep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "from karateclub.node_embedding.neighbourhood.grarep import GraRep\n",
    "\n",
    "G = nx.barbell_graph(m1=10, m2=4)\n",
    "draw_graph(G)\n",
    "\n",
    "gr = GraRep(dimensions=2,order=3)\n",
    "gr.fit(G)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "\n",
    "ida = 4\n",
    "idb = 5\n",
    "for x in G.nodes():\n",
    "    \n",
    "    v = gr.get_embedding()[x]\n",
    "    ax.scatter(v[ida],v[idb], s=1000)\n",
    "    ax.annotate(str(x), (v[ida],v[idb]), fontsize=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HOPE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "from gem.embedding.hope import HOPE\n",
    "\n",
    "G = nx.barbell_graph(m1=10, m2=4)\n",
    "draw_graph(G)\n",
    "\n",
    "hp = HOPE(d=4, beta=0.01)\n",
    "hp.learn_embedding(G)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "\n",
    "for x in G.nodes():\n",
    "    \n",
    "    v = hp.get_embedding()[x,2:]\n",
    "    ax.scatter(v[0],v[1], s=1000)\n",
    "    ax.annotate(str(x), (v[0],v[1]), fontsize=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DeepWalk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "from karateclub.node_embedding.neighbourhood.deepwalk import DeepWalk\n",
    "\n",
    "G = nx.barbell_graph(m1=10, m2=4)\n",
    "draw_graph(G)\n",
    "\n",
    "dw = DeepWalk(dimensions=2)\n",
    "dw.fit(G)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "\n",
    "for x in G.nodes():\n",
    "    \n",
    "    v = dw.get_embedding()[x]\n",
    "    ax.scatter(v[0],v[1], s=1000)\n",
    "    ax.annotate(str(x), (v[0],v[1]), fontsize=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Node2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "from node2vec import Node2Vec\n",
    "\n",
    "G = nx.barbell_graph(m1=10, m2=4)\n",
    "draw_graph(G)\n",
    "\n",
    "node2vec = Node2Vec(G, dimensions=2)\n",
    "model = node2vec.fit(window=10)\n",
    "embeddings = model.wv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "\n",
    "for x in G.nodes():\n",
    "    \n",
    "    v = model.wv[str(x)]\n",
    "    ax.scatter(v[0],v[1], s=1000)\n",
    "    ax.annotate(str(x), (v[0],v[1]), fontsize=16)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Edge2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from node2vec.edges import HadamardEmbedder\n",
    "edges_embs = HadamardEmbedder(keyed_vectors=model.wv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "\n",
    "for x in G.edges():\n",
    "    \n",
    "    v = edges_embs[(str(x[0]), str(x[1]))]\n",
    "    ax.scatter(v[0],v[1], s=1000)\n",
    "    ax.annotate(str(x), (v[0],v[1]), fontsize=16)\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Graph2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "from karateclub import Graph2Vec\n",
    "\n",
    "n_graphs = 20\n",
    "\n",
    "def generate_radom():\n",
    "    n = random.randint(6, 20)\n",
    "    k = random.randint(5, n)\n",
    "    p = random.uniform(0, 1)\n",
    "    return nx.watts_strogatz_graph(n,k,p), [n,k,p]\n",
    "\n",
    "Gs = [generate_radom() for x in range(n_graphs)]\n",
    "\n",
    "model = Graph2Vec(dimensions=2, wl_iterations=10)\n",
    "model.fit([x[0] for x in Gs])\n",
    "embeddings = model.get_embedding()\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(10,10))\n",
    "\n",
    "for i,vec in enumerate(embeddings):\n",
    "    \n",
    "    ax.scatter(vec[0],vec[1], s=1000)\n",
    "    ax.annotate(str(i), (vec[0],vec[1]), fontsize=40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
